// 在文件顶部添加
import SparkMD5 from 'spark-md5';
self.onmessage = function (event) {
    const file = event.data;
    const chunkSize = 1024 * 1024 * 4; // 每块10MB
    let currentChunk = 0;
    const spark = new SparkMD5.ArrayBuffer();
    const fileReader = new FileReader();
    let end = 0;

    function loadNext() {
        const start = currentChunk * chunkSize;
        end = start + chunkSize;
        if (end > file.size) {
            end = file.size;
        }
        fileReader.readAsArrayBuffer(file.slice(start, end));
    }

    fileReader.onload = function (e) {
        spark.append(e.target.result);
        currentChunk++;
        if (end < file.size) {
            loadNext();
        } else {
            const calculatedMD5 = spark.end();
            self.postMessage(calculatedMD5);
        }
    };

    fileReader.onerror = function (e) {
        self.postMessage({ error: e });
    };

    loadNext();
};